package com.kylin.cas;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

public class CASDemo {

    ////CAS CompareAndSet: 比较并交换
    //public static void main(String[] args) {
    //    AtomicInteger atomicInteger = new AtomicInteger(2020);
    //
    //    //期望，更新
    //    //如果我期望的值达到了，那么就更新，否则，就不更新
    //    System.out.println(atomicInteger.compareAndSet(2020, 2021));//true
    //    System.out.println(atomicInteger.get());//2021
    //
    //    System.out.println(atomicInteger.compareAndSet(2020, 2021));//false
    //    System.out.println(atomicInteger.get());//2021
    //}


    //CAS CompareAndSet: 比较并交换
    //public static void main(String[] args) {
    //    AtomicInteger atomicInteger = new AtomicInteger(2020);
    //
    //    //期望，更新
    //    //如果我期望的值达到了，那么就更新，否则，就不更新
    //    System.out.println(atomicInteger.compareAndSet(2020, 2021));//true
    //    System.out.println(atomicInteger.get());//2021
    //
    //    System.out.println(atomicInteger.compareAndSet(2021, 2020));//ture
    //    System.out.println(atomicInteger.get());//2020
    //
    //    System.out.println(atomicInteger.compareAndSet(2020, 6666));
    //    System.out.println(atomicInteger.get());
    //}

    public static void main(String[] args) {
        AtomicStampedReference<Integer> atomicInteger = new AtomicStampedReference<>(2020, 1);

        new Thread(() -> {
            int stamp = atomicInteger.getStamp();//获得版本号
        }, "a").start();

        new Thread(() -> {
            int stamp = atomicInteger.getStamp();//获得版本号
        }, "b").start();
    }
}
